//
//  ThumbnailListNavigationController.m
//  SecretAlbums
//
//  Created by Tinys on 15/6/28.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "ThumbnailListNavigationController.h"
#import "TransitionAnimation.h"
#import "ConfirmViewController.h"
#import "UIView+SecretAlbums.h"

@interface ThumbnailListNavigationController () <ConfirmViewControllerDelegate>

@property (nonatomic, strong) ConfirmViewController *confirmViewController;

@end

@implementation ThumbnailListNavigationController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationBar.barStyle = UIBarStyleBlack;
    self.toolbar.barStyle = UIBarStyleBlack;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    
    [self addNotificationObserver];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    
    [self removeNotificationObserver];
}

- (BOOL)shouldAutorotate
{
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationPortrait;
}

#pragma mark - 私有方法

- (void)openConfirmView
{
    if (!self.confirmViewController) {
        self.confirmViewController = [ConfirmViewController new];
        self.confirmViewController.delegate = self;
    }
    
    [self.confirmViewController setupBackgroundWithImage:[self.view currentScreenImg]];
    
    [self addChildViewController:self.confirmViewController];
    [self.view addSubview:self.confirmViewController.view];
    [self.confirmViewController didMoveToParentViewController:self];
    
    [self.confirmViewController.view setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.confirmViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
}

- (void)dismissConfirmView
{
    [self.confirmViewController willMoveToParentViewController:nil];
    [self.confirmViewController.view removeFromSuperview];
    [self.confirmViewController removeFromParentViewController];
}

- (void)applicationDidEnterBackground:(NSNotification *)notification
{
    [self openConfirmView];
}

#pragma mark - Notification Observer

- (void)addNotificationObserver
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center addObserver:self
               selector:@selector(applicationDidEnterBackground:)
                   name:UIApplicationDidEnterBackgroundNotification
                 object:nil];
}

- (void)removeNotificationObserver
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center removeObserver:self name:UIApplicationDidEnterBackgroundNotification object:nil];
}

#pragma mark - UIViewControllerTransitioningDelegate

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    TransitionAnimation *animation = [TransitionAnimation new];
    animation.isPositiveAnimation = YES;
    
    return animation;
}

- (id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    TransitionAnimation *animation = [TransitionAnimation new];
    animation.isPositiveAnimation = NO;
    
    return animation;
}

#pragma mark - ConfirmViewControllerDelegate

- (void)successConfirmViewController:(ConfirmViewController *)confirmViewController
{
    [self dismissConfirmView];
}

@end
